﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using NikeSale.Models;
using static NikeSale.Bll.DBSelect.Login;

namespace NikeSale.Ull
{
    public partial class LoginForm : Form
    {
        private int _imgId = 1;
        private int BackImageID
        {
            get
            {
                return _imgId;
            }
            set
            {
                
            }
        }

        public LoginForm()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
        }

        #region 点击事件
        //密码可见性
        bool isclose = true;
        private void pic_pwdLook_Click(object sender, EventArgs e)
        {
            if (isclose)
            {
                isclose = false;
                this.pic_pwdLook.BackgroundImage = fixImg.密码可见;
                this.tb_Pwd.UseSystemPasswordChar = false;
            }
            else
            {
                isclose = true;
                this.pic_pwdLook.BackgroundImage = fixImg.密码不可见;
                this.tb_Pwd.UseSystemPasswordChar = true;
            }
        }
        //登陆按钮+-
        private void pic_login_Click(object sender, EventArgs e)
        {
            this.pic_login.Focus();
            Login();
        }
        //按回车键登陆
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            this.lb_Account_error_prompt.Visible = false;
            this.lb_Password_error_prompt.Visible = false;
            if (e.KeyCode == Keys.Enter)
            {
                Login();
            }
        }

        //关闭按钮
        private void pic_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //最小化按钮
        private void pic_small_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion

        #region 悬浮动画
        //登陆窗体
        private void pic_login_MouseEnter(object sender, EventArgs e)
        {
            this.pic_login.BackgroundImage = fixImg.登陆被选中;
        }
        private void pic_login_MouseLeave(object sender, EventArgs e)
        {
            this.pic_login.BackgroundImage = fixImg.登陆;
        }
        private void pic_login_MouseDown(object sender, MouseEventArgs e)
        {
            this.pic_login.BackgroundImage = fixImg.登陆按下;
        }
        private void pic_login_MouseUp(object sender, MouseEventArgs e)
        {
            this.pic_login.BackgroundImage = fixImg.登陆被选中;
        }
        //关闭按钮
        private void pic_close_MouseEnter(object sender, EventArgs e)
        {
            this.pic_close.BackgroundImage = fixImg.关闭按钮被选中;
        }
        private void pic_close_MouseLeave(object sender, EventArgs e)
        {
            this.pic_close.BackgroundImage = fixImg.关闭按钮;
        }
        //最小化按钮
        private void pic_small_MouseEnter(object sender, EventArgs e)
        {
            this.pic_small.BackgroundImage = fixImg.最小化被选中;
        }
        private void pic_small_MouseLeave(object sender, EventArgs e)
        {
            this.pic_small.BackgroundImage = fixImg.最小化;
        }
        #endregion

        #region 其他事件
        //绘制圆角输入框
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Size size = new Size(this.tb_UserName.Size.Width + 20, this.tb_UserName.Size.Height + 20);
            Bitmap bitmap = DSAPI.图形图像.绘制圆角矩形(size, 10, Color.White, Color.DarkGray);
            e.Graphics.DrawImage(bitmap, new Point(this.tb_UserName.Location.X - 10, this.tb_UserName.Location.Y - 10));
            e.Graphics.DrawImage(bitmap, new Point(this.tb_Pwd.Location.X - 10, this.tb_Pwd.Location.Y - 10));
        }
        //密码和账号输入框获取焦点时清除错误提示
        private void tb_Pwd_Enter(object sender, EventArgs e)
        {
            this.lb_Account_error_prompt.Visible = false;
            this.lb_Password_error_prompt.Visible = false;
        }
        //移动事件穿透
        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            Methods.FormMove(this.Handle);
        }
        #endregion

        #region 窗体方法
        /// <summary>
        /// 显示错误提示信息
        /// </summary>
        /// <param name="label"></param>
        /// <param name="text"></param>
        private void ShowError(Label label,string text)
        {
            label.Text = text;
            label.Visible = true;
        }

        /// <summary>
        /// 登陆并打开主窗体
        /// </summary>
        /// <param name="info"></param>
        private void Login()
        {
            #region 条件过滤
            if (string.IsNullOrEmpty(this.tb_UserName.Text))
            {
                this.ShowError(this.lb_Account_error_prompt, "✖ 账号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(this.tb_Pwd.Text))
            {
                this.ShowError(this.lb_Password_error_prompt, "✖ 密码不能为空!");
                return;
            }
            #endregion


            LoginErrors res = UserSelect(this.tb_UserName.Text, this.tb_Pwd.Text);
            if (res == LoginErrors.UserNameError)
            {
                this.ShowError(this.lb_Account_error_prompt, "✖ 账号不存在！");
                return;
            }
            if (res == LoginErrors.PassWordError)
            {
                this.ShowError(this.lb_Password_error_prompt, "✖ 密码错误!");
                return;
            }
            #endregion

            #region 登陆成功
            this.lb_Password_error_prompt.Visible = false;
            this.lb_Account_error_prompt.Visible = false;
            Methods.ShowPrompt(" 登陆成功!", 1500);
            LoginInfo info = GetLoginInfo(this.tb_UserName.Text, this.tb_Pwd.Text);
            Animation.窗体动画.Leave_Animate(this.Handle, Animation.Leave_Animations.向内缩放, 1500);
            if (info.Role==Positions.收银员)
            {
                new CashierForm(info).ShowDialog();
            }
            else
            {
                new MainForm(info).ShowDialog();
            }
            Environment.Exit(0);
        }
        #endregion
    }
}
